package com.caojing.haolemai.ui.category.categoryadapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.caojing.haolemai.R;
import com.caojing.haolemai.bean.categorybean.ThirdBean;
import com.caojing.haolemai.ui.category.categorydclickactivity.CategoryClickActivity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/10/18.
 */
public class CategoryRightListGvAdapter extends BaseAdapter {

    private Context mContext;
    private List<ThirdBean> threadList;

    CategoryRightListGvAdapter(Context context, List<ThirdBean> list) {
        this.mContext = context;
        this.threadList = list;
    }

    @Override
    public int getCount() {
        return threadList.size();
    }

    @Override
    public Object getItem(int position) {

        return threadList.get(position);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.category_list_gv_item, parent, false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
             viewHolder = (ViewHolder) convertView.getTag();
        }

        final ThirdBean thirdBean = threadList.get(position);
        final String name = thirdBean.getVirtualName();
        String virtualIcon = thirdBean.getVirtualIcon();
        viewHolder.categoryTvGvList.setText(name);
        Glide.with(mContext).load(virtualIcon).into(viewHolder.categoryIvGvItem);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String brandId = thirdBean.getBrandId();
                String categoryId = thirdBean.getCategoryId();
                Intent intent = new Intent(mContext, CategoryClickActivity.class);
                intent.putExtra("brandName",name);
                intent.putExtra("brandId",brandId);
                intent.putExtra("categoryId",categoryId);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.category_iv_gv_item)
        ImageView categoryIvGvItem;
        @Bind(R.id.category_tv_gv_list)
        TextView categoryTvGvList;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
